﻿@Cofe ?= {}

$ = jQuery
utils = Cofe.utils

class EntrySearchView extends Spine.Controller    
  elements : 
    "#search" : "uiSearchBox"

  typingTimer : null
  doneTypingInterval = 2000 #time in ms

  init : () ->
    #http://stackoverflow.com/questions/4220126/run-javascript-function-when-user-finishes-typing-instead-of-on-key-up    
    @uiSearchBox.keyup(() => @typingTimer = setTimeout(@checkSearchBox, doneTypingInterval))
    @uiSearchBox.keydown(() => clearTimeout(@typingTimer))
  
  notifyPathChanged : (path) =>
    @uiSearchBox.val("")
    @uiSearchBox.Watermark("Search in " + utils.extractFilename(path))

  checkSearchBox : () =>
    newSearchStr = @uiSearchBox.val()
    if newSearchStr == "" or newSearchStr == null
      @trigger("searchCancelled", @)
    else @trigger("searchChanged", @, newSearchStr)

Cofe.EntrySearchView = EntrySearchView